package com.qf.week2.homework;

public class BookInterfaceImpl implements BookInterface {

    /**
     * 先定义一个数组做图书馆，能储存的量是200本
     */
    private Book[] bookArr = new Book[200];


    /**
     * 定义图书馆的格子，如果要改变书，就改变一个格子 为0既没有储存图书
     */
    private int size = 0;

    /**
     * 创建一本书
     */
//    Book book = new Book(size,);
    public Book[] getBookArr() {
        return bookArr;
    }

    public void setBookArr(Book[] bookArr) {
        this.bookArr = bookArr;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    /**
     * 根据bookId增加图书
     * @param book
     * @return
     */
    public boolean addBook(Book book){
        if (size < bookArr.length) {

            bookArr[size] = book;
            size++;

            return true; // 添加成功，返回true
        } else {
            System.out.println("图书管已满，无法添加更多图书。");
            return false; // 添加失败，返回false
        }
    }

    /**
     * 根据bookName查找图书
     * @param bookName
     * @return
     */
    public Book selectOne(String bookName){
        for (int i = 0; i < size; i++) {
            Book book = bookArr[i];
            if (book.getBookName().equals(bookName)) {
                System.out.println("您要找的图书是："+ bookArr[i]);
            }

        }

        return null;
    }


    /**
     *删除
     */
    public boolean deleteBook(String bookName){
        for (int i = 0; i < size; i++) {
            if (bookArr[i].getBookName().equals(bookName)) {
                for (int j = i; j < size - 1; j++) {
                    bookArr[j] = bookArr[j + 1];
                }
                size--;
                System.out.println("图书删除成功。");
                return true;
            }
        }
        System.out.println("未找到名字为 " + bookName + " 的图书。");
        return false;
    }


    /**
     * 更新
     * @param book
     * @return
     */
    public boolean updateBook(Book book){
        Book book1 = selectOne(book.getBookName());
        if (book1 != null) {
            book.setBookName(book.getBookName());
            book.setBookPage(book1.getBookPage());
            System.out.println("图书更新成功。");
            return true;
        } else {
            System.out.println("未找到ID为 " + book1.getBookName() + " 的图书。");
            return false;
        }
    }
    public Book selectAll(){
        for (int i = 0; i < size; i++) {
            System.out.println(bookArr[i]);

        }

        return null;
    }


    public static void printStringArr(String[] arr){
        System.out.print("[ ");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
        System.out.println("]");
    }
}
